﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.EntityClient;
using System.IO;
using System.Reflection;


namespace Communergy.Common
{
    public class ConfigurationHelper
    {
        //TODO - need to be able to choose between normal config and azure - abstract out? Has be to be used in Common
        public static string EntityFrameworkConnectionString
        {
            get
            {
                try
                {
                    string entityFrameworkConnectionString = ConfigurationManager.ConnectionStrings["communergyEntities"].ConnectionString;
                    return entityFrameworkConnectionString;
                }
                
                catch
                {
                    return null;
                }
                
            }
        }

        static public string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }

        public static string SQLLitePath 
        {
            get
            {
                string dbName = ConfigurationManager.AppSettings["SQLiteDatabaseName"];


                var absolutePath = Path.Combine(AssemblyDirectory, dbName);
                return absolutePath;
            }
             
        }


        public static string SqliteConnectionString (string absolutePath)  
        {
           
            EntityConnectionStringBuilder entityBuilder =  new EntityConnectionStringBuilder();
            entityBuilder.Provider ="System.Data.SQLite";
            entityBuilder.Metadata = "res://*/CommunergySQLite.csdl|res://*/CommunergySQLite.ssdl|res://*/CommunergySQLite.msl";

            entityBuilder.ProviderConnectionString = string.Format("Data Source={0}", absolutePath);
            string connectionString = entityBuilder.ToString();
            return connectionString;
               
        }


        
    }
}
